import { inject, ref } from 'vue';

const formContextKey = Symbol('formContext');

// 创建表单上下文
export const createFormContext = (options = {}) => {
    const context = {
        disabled: ref(false),
        validateAll: ref(false),
        requiredMessage: options.requiredMessage || '此字段为必填项',
        patternMessage: options.patternMessage || '格式不正确',

        // 表单验证方法
        validate: () => {
            context.validateAll.value = true;
        }
    };

    return {
        provide: () => provide(formContextKey, context),
        context
    };
};

// 使用表单上下文
export const useForm = () => {
    return inject(formContextKey);
};